package com.atguigu.service.impl;

import com.atguigu.entity.BaseCategory1;
import com.atguigu.entity.BaseCategory2;
import com.atguigu.entity.BaseCategory3;
import com.atguigu.entity.BaseCategoryEntity;
import com.atguigu.mapper.BaseCategory1Mapper;
import com.atguigu.service.BaseCategory1Service;
import com.atguigu.service.BaseCategory2Service;
import com.atguigu.service.BaseCategory3Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 一级分类表 服务实现类
 * </p>
 *
 * @author yangchaolove
 * @since 2021-11-06
 */
@Service
public class BaseCategory1ServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategory1Service {

    @Autowired
    private BaseCategory2Service category2Service;
    @Autowired
    private BaseCategory3Service category3Service;

    @Override
    public List<BaseCategoryEntity> listWithTree() {

        List<BaseCategory1> category1 = baseMapper.selectList(null);
        List<BaseCategoryEntity> category11 = category1.stream().map(e -> {
            BaseCategoryEntity baseCategoryEntity = new BaseCategoryEntity();
            baseCategoryEntity.setId(e.getId());
            baseCategoryEntity.setName(e.getName());
            return baseCategoryEntity;
        }).collect(Collectors.toList());

        List<BaseCategory2> category2 = category2Service.list(null);
        List<BaseCategoryEntity> category22 = category2.stream().map(e -> {
            BaseCategoryEntity baseCategoryEntity = new BaseCategoryEntity();
            baseCategoryEntity.setId(e.getId());
            baseCategoryEntity.setName(e.getName());
            baseCategoryEntity.setCategoryParentId(e.getCategory1Id());
            return baseCategoryEntity;
        }).collect(Collectors.toList());

        List<BaseCategory3> category3 = category3Service.list(null);
        List<BaseCategoryEntity> category33 = category3.stream().map(e -> {
            BaseCategoryEntity baseCategoryEntity = new BaseCategoryEntity();
            baseCategoryEntity.setId(e.getId());
            baseCategoryEntity.setName(e.getName());
            baseCategoryEntity.setCategoryParentId(e.getCategory2Id());
            return baseCategoryEntity;
        }).collect(Collectors.toList());

        List<BaseCategoryEntity> collect = category22.stream().map(e -> {
            List<BaseCategoryEntity> temp = category33.stream().filter(m -> {
                return m.getCategoryParentId() == e.getId();
            }).collect(Collectors.toList());
            e.setChildren(temp);
            return e;
        }).collect(Collectors.toList());
        List<BaseCategoryEntity> collect1 = category11.stream().map(e -> {
            List<BaseCategoryEntity> temp = collect.stream().filter(m -> {
                return m.getCategoryParentId() == e.getId();
            }).collect(Collectors.toList());
            e.setChildren(temp);
            return e;
        }).collect(Collectors.toList());

        return collect1;
    }



}
